package de.clapp.client.qrcodes;

import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.JScrollPane;

import de.clapp.panels.MyPanel;

@SuppressWarnings("serial")
public class CenterPanel extends MyPanel
{
    private BufferedImage image;
    private JScrollPane scrollPane;

    public CenterPanel()
    {
        BufferedImage tmpImage = null;
        try
        {
            tmpImage = ImageIO.read(new File("empty.png"));
            this.setPreferredSize(new Dimension(tmpImage.getWidth(), tmpImage.getHeight()));
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
        this.image = tmpImage;
    }

    public void setScrollPane(JScrollPane scrollPane)
    {
        this.scrollPane = scrollPane;
    }
    
    @Override
    public void paintComponent(Graphics g)
    {
        super.paintComponent(g);
        Dimension d = this.getSize();
        g.drawImage(this.image, (d.width - this.image.getWidth()) /2 , (d.height - this.image.getHeight()) /2, null);
        this.setPreferredSize(new Dimension(this.image.getWidth(), this.image.getHeight()));
        this.scrollPane.revalidate();
    }

    public void imageChanged(BufferedImage image)
    {
        this.image = image;
        repaint();
    }
}
